﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

public partial class Admin_ImgNewsList : System.Web.UI.Page
{
    int m_ColID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListItem li = new ListItem("栏目名称", "0");
            DropDownList1.Items.Add(li);
            li = new ListItem("阿城警事", "1");
            DropDownList1.Items.Add(li);
            li = new ListItem("图片新闻", "2");
            DropDownList1.Items.Add(li);
            DropDownList1.SelectedIndex = 0;
            BindDisplayList();
            
        }
    }
    private void BindDisplayList()
    {        
        string sql;
        if (DropDownList1.SelectedIndex == 0)
            sql= "select * from ImgNews ORDER BY [Top] DESC, EditDate DESC";
        else
            sql = "select * from ImgNews where Category='" + DropDownList1.SelectedValue + "' ORDER BY [Top] DESC, EditDate DESC";
        DataTable tb = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql);
        GridView1.Columns[m_ColID].Visible = true;
        GridViewOperation.BindPagingGridView(GridView1, tb.DefaultView, 0);
        GridView1.Columns[m_ColID].Visible = false;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            GridViewOperation.AddConfirmGridView(e, 6, "确定要删除么?删除后无法回复!");
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "New":
                Response.Redirect("~/Admin/ImgNewsEdit.aspx");
                break;
            case "Select":
                ImgNewsData ind = new ImgNewsData();
                ind.ID = new Guid(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[m_ColID].Text);
                string str = ((DataBoundLiteralControl)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Controls[0]).Text;
                if (str.IndexOf("<img src='img/top.gif' />")!=-1)
                    ind.Top = true;
                else
                    ind.Top = false;
                ImgNewsDataDBOption.SetTop(ind);
                Response.Redirect("~/Admin/ImgNewsList.aspx");
                break;

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Guid ID = new Guid(GridView1.Rows[e.RowIndex].Cells[m_ColID].Text);
        ImgNewsData imd=new ImgNewsData();
        imd.ID = ID;
        ImgNewsDataDBOption.Delete(imd);        
        BindDisplayList();
        //ScriptManager.RegisterStartupScript(Master.FindControl("UpdatePanel1"), this.GetType(), "AddNewsOk", "alert('删除成功!'); location.href='FlashManage.aspx'", true);
        //Response.Redirect("~/Admin/ImgNewsEdit.aspx");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect("~/Admin/ImgNewsEdit.aspx?id=" + GridView1.Rows[e.NewEditIndex].Cells[m_ColID].Text);
    }
    public string GetTopImage(object dataItem)
    {
        Boolean IsTop = Convert.ToBoolean(DataBinder.Eval(dataItem, "Top").ToString());
        if (IsTop)
            return "<img src='img/top.gif' />";
        else
            return "";
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDisplayList();       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/ImgNewsEdit.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/SearchList.aspx?search=" + Server.UrlEncode(TextBox1.Text));
    }
}